angular.module('starter.controllers')
.controller('ProductdetailCtrl', function($scope, museumFac, $stateParams, $timeout, $location, cfg, $cookies, uploadImg) {
    $scope.url = window.location.href;
    $cookies.put('lastPage', $scope.url);
    $scope.gotoCenter = function(){
        museumFac.user.usertest(function(data){
            if(data.info == true){
                $location.url('center');
            }else{
                $scope.url = window.location.href;
                $cookies.put('lastPage', $scope.url);
                $location.url('login');
            }
        });
    }

    $timeout(function() {
        var mySwiper = new Swiper('.swiper-container',{
          pagination : '.swiper-pagination',
          //pagination : '#swiper-pagination1',
        })
    }, 500);
    
	$scope.change_language = function(id){
	    $scope.language = id;
	    var expireDate = new Date();  
		expireDate.setDate(expireDate.getDate() + 30);                
		$cookies.put('language', id, {'expires': expireDate.toUTCString()});
		location.reload();
		if(id == 0){
			document.getElementById('title').innerHTML = '提示';
			document.getElementById('windowclosebutton').value = '确定';
		}else{
			document.getElementById('title').innerHTML = 'Hint';
			document.getElementById('windowclosebutton').value = 'Confirm';
		}

	}

	$scope.getLanguage = function(){
    	$scope.language = $cookies.get('language');
    	if($scope.language){
      		$scope.language = $scope.language;
    	}else{
      		$scope.language = 0;
    	}
    	if($scope.language == 0){
			document.getElementById('title').innerHTML = '提示';
			document.getElementById('windowclosebutton').value = '确定';
		}else{
			document.getElementById('title').innerHTML = 'Hint';
			document.getElementById('windowclosebutton').value = 'Confirm';
		}
  	}
  	$scope.getLanguage();

	$scope.checkOpenid = function(){
        $scope.url = window.location.href;

        museumFac.user.usertest(function(data){
            $scope.is_login = data.info;
            if($scope.is_login){
                $scope.getCart();
            }
          },function(data){
            alert(data.data.info);
        });

        wx.ready(function(){
          wx.onMenuShareTimeline({
            title: $scope.product.name + ' | 艺术商店\r\n四方当代美术馆', // 分享标题
            link: 'http://store.sifangartmuseum.org.cn/home/index/productDetail/productId/'+$stateParams.id, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: cfg.baseImgUrl + $scope.product.cover_paths, // 分享图标
            success: function () { 
                // 用户确认分享后执行的回调函数
            },
            cancel: function () { 
                // 用户取消分享后执行的回调函数
            }
        });
        // 获取“分享给朋友”按钮点击状态及自定义分享内容接口
        wx.onMenuShareAppMessage({
            title: $scope.product.name + ' | 艺术商店',  // 分享标题
            desc: '四方当代美术馆', // 分享描述
            link: 'http://store.sifangartmuseum.org.cn/home/index/productDetail/productId/'+$stateParams.id, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: cfg.baseImgUrl + $scope.product.cover_paths, // 分享图标
            type: '', // 分享类型,music、video或link，不填默认为link
            dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
            success: function () { 
                // 用户确认分享后执行的回调函数
            },
            cancel: function () { 
                // 用户取消分享后执行的回调函数
            }
          });
        });
	}
	

    $scope.getInfo = function(){
        museumFac.product.product({
            productId:$stateParams.id
        },function(data){
            $scope.product = data.info;
            $scope.lists = $scope.product.attributions;

            angular.forEach($scope.lists, function(key,item){
                item.is_chosen = 0;
            });

            $scope.lists[0].is_chosen = 1;
            $scope.ticket = $scope.lists[0];

            $scope.checkOpenid();

        },function(data){
            alert(data.data.info);
        });

    }
    $scope.getInfo();

    $scope.choose_ticket = function(key,list){
        angular.forEach($scope.lists, function(item){
          item.is_chosen = 0;
        });
        list.is_chosen = 1;
        $scope.ticket = list;
    }

    $scope.getCart = function(){
        museumFac.product.mycart(function(data){
            $scope.orders = data.info;

          $scope.allprice = 0;
          $scope.allquantity = 0;
          angular.forEach($scope.orders, function(item){
            item.quantity = parseInt(item.quantity, 10);
            if(item.activity_price != 0){
              $scope.allprice = $scope.allprice + item.activity_price*item.quantity;
            }else{
              $scope.allprice = $scope.allprice + item.price*item.quantity;
            }
            
            $scope.allquantity = $scope.allquantity + item.quantity;
          });
        },function(data){
            // alert(data.data.info);
            $location.url('login');
        });
    }
    

    $scope.addCart = function(){
        $scope.getCart();
        museumFac.product.addtocart({
            attributionId:$scope.ticket.id,
            quantity:1
        },function(data){
              if($scope.language == 0){
                alert("加入成功!");
              }else{
                alert("Success");
              }
            $scope.getCart();
        },function(data){
            alert(data.data.info);
        });
    }

    $scope.gotoCart = function(){
        // $location.url('cart');
        window.location.href = 'http://store.sifangartmuseum.org.cn/Public/museum/www/#/cart';
        event.stopPropagation();
    }

    $scope.backhome = function(){
      $location.url('home');
    }
  
})